Option Explicit On 

Imports System.Text.RegularExpressions

Public Class RegExFunctions

   '''''''''''''''''''''''''''''''''''''
   ' Public Methods
   '''''''''''''''''''''''''''''''''''''

   Shared Function IsMatch(ByVal Text As String, ByVal Pattern As String) As Boolean

      Dim Matches As MatchCollection
      Matches = GetMatches(Text, Pattern)

      IsMatch = Matches.Count > 0

   End Function

   Shared Function GetSubMatch(ByVal Text As String, ByVal Pattern As String) As String

      Dim Matches As MatchCollection
      Matches = GetMatches(Text, Pattern)

      If Matches.Count > 0 Then
         If Matches(0).Groups.Count > 0 Then
            Return Matches(0).Groups(1).ToString()
         End If
      End If

      Return ""

   End Function

   Shared Function GetSubMatchesAsString(ByVal Text As String, ByVal Pattern As String) As String

      Dim Matches As MatchCollection
      Matches = GetMatches(Text, Pattern)

      Dim temp As String = ""
      If Matches.Count > 0 Then
         Dim Match As Match
         For Each Match In Matches
            temp = temp & Match.Groups(1).ToString() & " "
         Next
      End If

      GetSubMatchesAsString = Trim(temp)

   End Function

   Shared Function GetMatch(ByVal Text As String, ByVal Pattern As String) As String

      Dim Matches As MatchCollection
      Matches = GetMatches(Text, Pattern)
      If Matches.Count > 0 Then
         Return Matches(0).ToString()
      End If

      Return ""

   End Function


   Shared Function GetMatches(ByVal Text As String, ByVal Pattern As String) As MatchCollection

      Dim RE As Regex = New Regex(Pattern, RegexOptions.IgnoreCase)
      Return RE.Matches(Text)

   End Function

   Shared Function Replace(ByVal Text As String, ByVal Pattern As String, ByVal ReplaceText As String) As String

      Dim RE As Regex = New Regex(Pattern, RegexOptions.IgnoreCase)
      Return RE.Replace(Text, ReplaceText)

   End Function

End Class
